package com.tencent.crazypenguin.dao;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.query.Query;

import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;

public class UserDao extends BaseDao<User> {
	
	@SuppressWarnings("rawtypes")
	public int authority(String loginName,String passWord) {
		
		Session s = HbmUtil.getSession();
		
		try{
			
			s.beginTransaction();
			
			Query query = s.createQuery("from User where userName=:userName");
			
			query.setParameter("userName", loginName);
			
			User user = (User) query.getSingleResult();
			
			s.getTransaction().commit();
			
			if(user == null){
				return ResultType.USERNAME_NOT_EXIST;
			}
			
			if(user.getPwd().equals(passWord)){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.setAttribute("user", user);
				return ResultType.LOGIN_SUCEESS;
			}
			else{
				return ResultType.INVALID_PSW;
			}
	
		}catch(Exception e){
			
			e.printStackTrace();
			return ResultType.SERVER_EXCEPTION;
			
		}finally{
			if(s!=null){
				s.close();
			}

		}

	}

}
